<?php
declare (strict_types = 1);

namespace app\index\model;

use think\Exception;
use think\facade\Config;
use think\facade\Request;
use think\Model;

/**
 * @mixin \think\Model
 */
class GoodsProp extends Model
{
    public static $propValueArr;
    protected $pk="pid";
    protected $error;

    public function getError(){
        return $this->error;
    }
    public function SearchPropValueAttr($query,$value,$data){
        if(is_array($value) && count($value)>0){
                        foreach($value as $v){
            $query->whereFindInSet('pvalues',$v);
            }
        }
    }
    public function SearchPnameAttr($query,$value,$data){
        if(''!==$value){
            $query->where('pname','like','%'.$value.'%');
        }
    }

    public function getStatusAttr($value,$data){
        $ret = [0 => '否',1=>'是'];
        return $ret[$data['status']];

    }

    public function getPvaluesZnAttr($value,$data){
        $arr=self::$propValueArr;

        $arrTmp=explode(',',$data['pvalues']);

        $pvalues_zn='';

        foreach ($arrTmp as $v){
            $pvalues_zn .=$arr[$v] . ',';
        }
        $pvalues_zn=trim($pvalues_zn,',');
      return $pvalues_zn;

//       $pv=GoodsPropValue::getPropValue();
//        $arr_Tmp = explode(',',$data['pvalues']);
//      $str='';
//      foreach ($pv as $k=>$v){
//          if(in_array($k,$arr_Tmp)){
//              $str.=$v.',';
//          }
//      }
//      $str=trim($str,',');
//      return $str;

    }

    public static function onAfterRead(Model $model)
    {
        self::$propValueArr = GoodsPropValue::getPropValue();
    }

    public static function getGoodsProp($data=[],$query=[],$page=false,$pv=[])
    {
        $model=self::field(true)->order('sort_order')->withSearch(array_keys($data),$data);
//        $paginate=$model->paginate(['list_row'=>10,'query'=>$query])->toArray();
//        if($paginate['total']%$paginate['per_page']==0 and $page>1){
//            $page=$page-1;
//        }


        $list_rows = Config::has('view.list_rows')?Config::get('view.list_rows'):15;
        $urlPage=Request::get('page',1);
        $lastPage=$model->paginate(['list_rows'=>$list_rows,'query'=>$query])->toArray()['last_page'];
        if($urlPage > $lastPage){
            $urlPage = $lastPage;
        }
    //$arr_Tmp=[];
        if($page){
            $result=$model->select();
        }else{
            $result=$model->paginate([
                'list_rows' =>$list_rows,
                //'page'=>$page,
                'fragment'=>'top',
                'query'=>$query,
                'page'=>$urlPage,
            ]);
        }
        return $result;

    }
    public function add(array $data, array $allowField = [], bool $replace = false) {

        try{
            if(empty($data)){
                $this->error = '新增数据不能为空！';
                return ;
            }
            // 限制写入的字段列表
            if (!empty($allowField))    $this->allowField($allowField);

            $this->replace($replace)->save($data);

            return true;
        }catch (\Exception $e){
            $this->error = $e->getMessage();
            return false;
        }
    }

    public function getInsertId(){
        $data = $this->toArray();
        return $data[$this->pk];
    }

    public function setPvaluesAttr($value,$data){
        if ($value!==""){
            $data['pvalues']=implode(',',$data['pvalues']);
            return $data['pvalues'];
        }

    }
//修改
    public function edit($data, $where = [], $allowField = []){

        try{
            if(empty($data)){
                $this->error = "修改数据不能为空!";
                return ;
            }

            if (!empty($allowField)) {
                $this->allowField($allowField);
            }

            if (!empty($where)) {
                $this->setUpdateWhere($where);
                if(isset($data[$this->pk]))     unset($data[$this->pk]);
            }

            return $this->exists(true)->save($data);

        }catch (\Exception $e){
            $this->error = $e->getMessage();
            return false;
        }
    }
    

    //

}
//->each(function($item) use($pv,$arr_Tmp,$data){
//    $arr_Tmp = explode(',',$item['pvalues']);
//    $item['pvalues_zn']='';
//    foreach ($pv as $k=>$v){
//        foreach ($arr_Tmp as $tv){
//            if($tv == $k){
//                $item['pvalues_zn'].= $v.',';
//            }
//        }
//    }
//    $item['pvalues_zn']=trim($item['pvalues_zn'],',');
//
//
//})




//$db = Db::table('ecm_goods_prop')->field(true);
//if(is_array($keyword) && count($keyword)>0){
//    foreach($keyword as $v){
//        $db->where('pvalues','find in set',$v);
//    }
//}
//View::assign('keyword',$keyword);
//
//if('' !== $pname){
//    $db->where('pname','like','%'.$pname.'%');
//}
//View::assign('pname',$pname);
//$sx=$db->paginate([
//    'list_rows'=>3,
////                'var_page'=>'p',
////                'page' =>3,
//    'fragment'=>'top',
//    'query'=>input('get.'),
//]);
//$page=$sx->render();
//View::assign('page',$page);
//
//$sx=$sx->toArray()['data'];
//
//


